let BaseForm = require('@/common/vue/BaseForm');
let comDriver = require('@/common/driver');

class Bean extends BaseForm{

    async buildControl() {
        await super.buildControl();

        let { createApp } = require('vue/dist/vue.esm-bundler.js');
        let vant = require('vant/lib/vant.min.js');

        let app = createApp({
            data:
                () => {
                    let data = {};
                    this.buildData(data);
                    this.param.controls.forEach(function (control, key, map) {
                        control.buildData(data);
                    }, this);
                    return data;
                },
            components: this.param.vComponent,
            methods: this.param.vMethod,
            computed: this.param.vComputed,
        });

        app.use(vant);

        this.param.vue = app.mount(this.getTemplateElementID());

        this.buildControlAfter();
    }

    async showStyle() {
        await super.showStyle();
        require('vant/lib/index.css');
    }

    showError(message, duration = 3, onClose = null) {
        this.param.loader_showing = false;
        this.param.vue.$toast.fail({
            duration: duration * 1000,
            message: message,
        });
        if (!onClose) return;
        let timer = setInterval(() => {
            clearInterval(timer);
            onClose();
        }, duration * 1000);
    }

    showMessage(message, duration = 3, onClose = null) {
        this.param.loader_showing = false;
        this.param.vue.$toast({
            duration: duration * 1000,
            message: message,
        });
        if (!onClose) return;
        let timer = setInterval(() => {
            clearInterval(timer);
            onClose();
        }, duration * 1000);
    }

    showLoading(message, bool) {
        this.param.loader_showing = this.param.vue.$toast.loading({
            duration: 0,
            message: message || '通讯中...',
        });
    }

    hideLoading(dir = false) {
        if (this.param.loader_showing) this.param.vue.$toast.clear();
    }
}


module.exports = Bean;
